package com.baizhi.service.impl;

import com.baizhi.entity.Book;
import com.baizhi.mapper.BookMapper;
import com.baizhi.mapper.ItemMapper;
import com.baizhi.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper mapper;
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public PageInfo<Book> showPageBooks(Integer pageNum,Integer pageSize,String query,String value) {
        return new PageInfo<>(mapper.selectAllBooks(pageNum,pageSize,query,value),10);
    }

    @Override
    public Book showOne(Integer id) {
        return mapper.selectBookById(id);
    }

    @Override
    public void addBook(Book b) {
        mapper.addBook(b);
    }

    @Override
    public void updateBook(Book book) {
        mapper.updateBook(book);
    }

    @Override
    public void removeBook(Integer id) {
        if(itemMapper.selectCount(id)!=null){
            throw new RuntimeException("该图书正在销售，无法删除");
        }
        mapper.removeBook(id);
    }

    @Override
    public List<Book> recommends() {
        return mapper.recommendBooks();
    }

    @Override
    public List<Book> hotSale() {
        return mapper.hotSale();
    }

    @Override
    public List<Book> newBooks() {
        return mapper.newBooks();
    }

    @Override
    public List<Book> newHotBooks() {
        return mapper.newHotBooks();
    }

    @Override
    public PageInfo<Book> selectBooks(Integer pageNum, Integer pageSize,String value) {
        return new PageInfo<>(mapper.selectBooks(pageNum,pageSize,value));
    }

    @Override
    public PageInfo<Book> selectBookByCategoryId(Integer pageNum, Integer pageSize, Integer fid,Integer sid) {
        return new PageInfo<>(mapper.selectBookByCategoryId(pageNum,pageSize,fid,sid));
    }
}
